package com.music.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.music.server.pojo.*;
import com.music.server.mapper.ConsumerMapper;
import com.music.server.service.CollectService;
import com.music.server.service.CommentService;
import com.music.server.service.ConsumerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.music.server.service.RankService;
import com.music.server.vo.ConsumerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 前端用户 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-03-05
 */
@Service
public class ConsumerServiceImpl extends ServiceImpl<ConsumerMapper, Consumer> implements ConsumerService {

    @Autowired
    private CollectService collectService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private RankService rankService;

    @Override
    public Map<String, Object> page(Integer pageNum, Integer size, ConsumerVo consumerVo) {
        Page<Consumer> singerPage = new Page<>(pageNum, size);
        QueryWrapper<Consumer> consumerQueryWrapper = new QueryWrapper<>();
        if (consumerVo.getUsername() != null) {
            consumerQueryWrapper.like("username", consumerVo.getUsername());
        }
        if (consumerVo.getSex() != null) {
            consumerQueryWrapper.eq("sex", consumerVo.getSex());
        }
        this.page(singerPage, consumerQueryWrapper);
        Map<String, Object> data = new HashMap<>();
        data.put("total", singerPage.getTotal());
        data.put("list", singerPage.getRecords());
        return data;
    }

    @Override
    @Transactional
    public boolean deleteById(Integer id) {
        //删前端用户
        this.removeById(id);
        //删用户收藏的
        QueryWrapper<Collect> collectQueryWrapper = new QueryWrapper<>();
        collectQueryWrapper.eq("user_id", id);
        collectService.remove(collectQueryWrapper);
        //删用户评论
        QueryWrapper<Comment> commentQueryWrapper = new QueryWrapper<>();
        commentQueryWrapper.eq("user_id", id);
        commentService.remove(commentQueryWrapper);

        //删用户评价
        QueryWrapper<Rank> rankQueryWrapper = new QueryWrapper<>();
        commentQueryWrapper.eq("consumer_id", id);
        rankService.remove(rankQueryWrapper);

        return true;


    }
}
